package com.platform.common.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

/**
 * map工具类
 * @desc com.netbox.utils
 * @time 2017-8-2上午9:03:01
 */
public class MapUtils {

	private static Logger log = Logger.getLogger(MapUtils.class);
	
	public static void main(String[] args) {
		Map<String,String[]> map = new HashMap<String,String[]>();
		map.put("name", new String[]{"zhangsan"});
		map.put("like", new String[]{"pingpang","basketball"});
		Map<String, String> map2 = getParameterMap(map);
		for (Entry<String, String> entry : map2.entrySet()) {
			System.out.println("key:"+entry.getKey() +",value："+entry.getValue());
		}
	}
	
	/**
	 * 从request中获得参数Map，并返回可读的Map
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, String> getParameterMap(Map properties) {
		// 返回值Map
		Map<String, String> returnMap = new HashMap<String, String>();
		// 参数Map
		Iterator entries = properties.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry entry = (Map.Entry) entries.next();
			String name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			String value = "";
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value += values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			returnMap.put(name, value);
		}
		return returnMap;
	}
	
}
